const Router = require('@koa/router');
const {UserModel} = require('../database/index')
const router = new Router();


router.get('/getAllUsers', (ctx, next) => {
    ctx.body = {
        code: 200,
        msg: '获取数据成功',
        data: [
            { name: 'zs', age: 21 },
            { name: 'lishi', age: 22 },
        ]
    }
})

router.post('/register', async (ctx, next) => {
    const { username, password } = ctx.request.body
    console.log(111111111111,username,password)
    try {
        const result = await UserModel.create({ username,password})
        console.log(result.toJSON())
        ctx.body = {
            code: 200,
            msg: '登录成功'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            msg: '注册失败'
        }
    }

})

router.post('/login', (ctx, next) => {
    const { name, password } = ctx.request.body
    if (name == 'zs' && password == '123456') {
        ctx.body = {
            code: 200,
            msg: '登录成功'
        }
    } else {
        ctx.body = {
            code: 501,
            msg: '登录失败'
        }
    }

})

module.exports = router
